<template>
	<view class="flex-column">
		<view class="content">
			<image class="logo" src="../../static/imgs/logo.png" />
			<view class="mainInfo">
				<!-- <text class="title">{{ info }}</text> -->
				<!-- <text class="tip">\n{{ Mtip }}</text> -->
			</view>
		</view>
		<!--    <view>
      <view class="infoContentTitle">
        <image class="infoPic" src="../../static/info-circle.png"/>
        <text class="infoTitle">更新摘要</text>
      </view>
      <view class="infoContent">
        <text class="updateInfo">{{ updateInfo }}</text>
      </view>
    </view> -->
		<!--    <view v-if="!currentIsLatest">
      <view class="infoContentTitle">
        <image class="infoPic" src="../../static/sync.png"/>
        <text class="infoTitle">更新大小</text>
      </view>
      <view class="infoContent">
        <text class="updateInfo">{{ packgeSize }}</text>
      </view>
    </view> -->
		<view class="minorContent ">
			<view class="smallTitle">
				<text>下载进度:{{ downloadProgress }}%</text>
				<progress :percent="downloadProgress" stroke-width="4" />
			</view>
			<!-- <button v-if="!startProgress && !currentIsLatest" type="primary" @click="handleUpdate()">立即更新</button>
      <button v-if="currentIsLatest" :loading="buttonLoading" type="primary" @click="getLatest()">检查更新</button> -->
		</view>
	</view>
</template>

<script>
	import {resUrlBase}  from '../../serveices/util.js'
	const downloadLink = resUrlBase+"upgrade.ashx?action=download&version=lastest"
	export default {
		data() {
			return {
				info: '正在加载', // 主标题显示版本号
				startProgress: false, // 下载进行 - 标记
				downloadProgress: 0
			}
		},
		onReady() {
			this.startUpdate()
		},
		// 用户关闭页面时检查是否存在下载任务
		onUnload() {
			if (this.downloadTask) {
				this.stopUpdate()
				// this.showToast('更新被取消')
			}
		},
		methods: {
			stopUpdate() {
				this.downloadTask.abort()
				this.downloadTask = null
				this.startProgress = false
			},
			startUpdate() {
				console.log("开始升级")
				this.downloadTask = uni.downloadFile({
					url: downloadLink,
					success: (res) => {
						if (res.statusCode === 200) {
							// 保存下载的安装包
							uni.saveFile({
								tempFilePath: res.tempFilePath,
								success: (res) => {
									this.packgePath = res.savedFilePath
									// // 进行安装
									// this.installPackge()
									// 安装更新
									plus.runtime.install(this.packgePath, {
										force: true
									})
									// 任务完成，关闭下载任务
									this.stopUpdate()
								}
							})
						}
					},
					fail(err) {
						console.log(err)
					}
				})
				// 进度条更新
				this.downloadTask.onProgressUpdate((res) => {
					// console.log("进度条更新",res)
					this.downloadProgress = res.progress
				})
			},
			getDownloadUrl(){
				
			}

		}
	}
</script>

<style>
	.flex-column {
		display: flex;
		flex-direction: column;
	}

	.content {
		text-align: center;
	}

	.minorContent {
		width: 650upx;
		padding: 0 50upx;
	}

	.process {
		margin-top: 200upx;
		margin-left: 30%;
	}

	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 100upx;
	}

	.title {
		font-size: 36upx;
		color: #373737;
		font-weight: bold;
	}

	.infoTitle {
		font-size: 32upx;
		color: #373737;
		padding-left: 15upx;
		font-weight: bold;
	}

	.tip {
		font-size: 28upx;
		color: #7E7E83;
		vertical-align: text-top;
	}

	.bottom_aera {
		position: absolute;
		bottom: 0;
		height: 12%;
	}

	.line {
		padding: 0 600upx;
		border-bottom: 2upx solid #D8D8D8;
	}

	.infoPic {
		height: 50upx;
		width: 50upx;
	}

	.infoContentTitle {
		display: flex;
		align-items: center;
		padding: 40upx 40upx;
	}

	.infoContent {
		display: flex;
		align-items: center;
	}

	.updateInfo {
		font-size: 28upx;
		color: #7E7E83;
		padding: 0 80upx;
	}

	.smallTitle {
		font-size: 26upx;
		font-weight: 500;
		padding: 20upx 0;
		line-height: 1.5;
		color: #888;
	}
</style>
